package dhm.com.hpx.activity;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.hpx.R;
import dhm.com.hpx.base.BaseActivity;
import dhm.com.hpx.base.Presenter.PressenterImpl;
import dhm.com.hpx.base.netWork.Constant;
import dhm.com.hpx.base.netWork.LoginContract;
import dhm.com.hpx.entity.Bean;
import dhm.com.hpx.utils.SpUtils;

public class AddspeechcraftActivity extends BaseActivity implements LoginContract.IView {

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.edit_content)
    EditText editContent;
    //0:添加话术分类  1:添加话术
    private String cat_id;

    private PressenterImpl pressenter;
    private String uid;
    private String speechcraft_id;
    private String type;
    private String cat_name;
    private String content;

    @Override
    protected int getLayout() {
        return R.layout.activity_addspeechcraft;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        uid = SpUtils.getString(this, "uid");
        pressenter = new PressenterImpl();
        pressenter.attachView(this);
        type = getIntent().getStringExtra("type");


        switch (type){
            case "1":
                //添加话术分类
                title.setText("增加话术分类");
                break;
            case "2":
                //修改话术分类
                title.setText("修改话术分类");
                cat_id = getIntent().getStringExtra("cat_id");
                cat_name = getIntent().getStringExtra("cat_name");
                editContent.setText(cat_name);
                break;
            case "3":
                //添加话术
                title.setText("添加话术");
                cat_id = getIntent().getStringExtra("cat_id");
                break;
            case "4":
                //修改话术
                title.setText("修改话术");
                speechcraft_id = getIntent().getStringExtra("speechcraft_id");
                content = getIntent().getStringExtra("content");
                editContent.setText(content);
                break;
                default:
        }
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.back, R.id.commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.commit:
                String s = editContent.getText().toString();
                if (s==null||s.equals("")){
                    Toast.makeText(this, "请检查数据", Toast.LENGTH_SHORT).show();
                    return;
                }

                Map<String,String> map=new HashMap<>();
                map.put("token", Constant.TOKEN);
                map.put("uid",uid);
                switch (type){
                    case "1":
                        //添加话术分类
                        map.put("cat_name",s);
                        pressenter.sendMessage(this,Constant.add_cat,map, Bean.class);
                        break;
                    case "2":
                        //修改话术分类
                        map.put("cat_id",cat_id);
                        map.put("cat_name",s);
                        pressenter.sendMessage(this,Constant.modify_cat,map, Bean.class);
                        break;
                    case "3":
                        //添加话术
                        map.put("cat_id",cat_id);
                        map.put("content",s);
                        pressenter.sendMessage(this,Constant.speechcraftadd,map,Bean.class);
                        break;
                    case "4":
                        //修改话术
                        map.put("content",s);
                        map.put("speechcraft_id",speechcraft_id);
                        pressenter.sendMessage(this,Constant.speechcraftmodify,map, Bean.class);
                        break;
                    default:
                }
                break;
            default:
        }
    }

    @Override
    public void requesta(Object data) {
        if (data instanceof Bean){
            Bean bean= (Bean) data;
            Toast.makeText(this, bean.getMessage(), Toast.LENGTH_SHORT).show();
            if (bean.getCode()==1){
                finish();
            }

        }
    }

    @Override
    public void fail(String error) {

    }
}
